<!--
 * @Descripttion: 
 * @Author: dedeye
 * @Date: 2020-02-26 20:04:00
 * @LastEditors: dedeye
 * @LastEditTime: 2020-02-27 23:11:49
 -->
 <!DOCTYPE html>
 <html>
 <head>
     <meta charset="utf-8">
     <title>layui</title>
     <meta name="renderer" content="webkit">
     <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
     <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
     <link rel="stylesheet" href="__ADMIN__/lib/layui-v2.5.5/css/layui.css" media="all">
     <link rel="stylesheet" href="__ADMIN__/css/public.css" media="all">
     <style>
         body {
             background-color: #ffffff;
         }
     </style>
 </head>
 <body>
 <div class="layui-form layuimini-form" lay-filter="editForm">
    <div class="layui-form-item">
        <label class="layui-form-label">选择权限</label>
        <div class="layui-input-block">
            <div id="LAY-auth-tree-index"></div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
 </div>
 </div>
<script src="__ADMIN__/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="__ADMIN__/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
 <script>
    layui.use(['form', 'authtree'], function () {
        var form = layui.form,
            layer = layui.layer,
            authtree = layui.authtree,
            $ = layui.jquery;

        var collectionId = parent.$('#hideCollectionId').val();
        $.ajax({
            url: '/admin/group/authtree/' + collectionId,
            success: function(res) {
                if(res.code < 0) {
                    layer.msg(res.msg, {icon: 5,time: 500,shade: 0.1});
                    return false;
                }
                console.log(res.data.checkedId)
                const trees = authtree.listConvert(res.data.list, {
                    primaryKey: 'id',
                    startPid: 0,
                    parentKey: 'fid',
                    nameKey: 'name',
                    valueKey: 'id',
                    checkedKey: res.data.checkedId
                });
                authtree.render('#LAY-auth-tree-index', trees, {
                    inputname: 'authids[]', 
                    layfilter: 'lay-check-auth', 
                    autowidth: true,
                });
            }
        })
         //监听提交
         form.on('submit(saveBtn)', function (data) {
            let authids = authtree.getChecked('#LAY-auth-tree-index');
            $.ajax({
                url: '/admin/group/save-auth',
                data: {
                    id: collectionId,
                    authids: authids
                },
                method: 'post',
                success: function (res) {
                    layer.msg(res.msg, {
                        icon: res.code < 0 ? 2 : 1,
                        time: 800,
                        shade: 0.1,
                        end: function() {
                            if(res.code > 0) {
                                closeThis();
                            }
                        }
                    });
                }
            })
         });
 
         function closeThis(re = false)
         {
             var iframeIndex = parent.layer.getFrameIndex(window.name);
             if(re)
                 parent.$('#isReload').val('yes');
             parent.layer.close(iframeIndex);
         }
     });
 </script>
 </body>
 </html>